package com.chx.controller;

import com.chx.dto.CustomerDto;
import com.chx.dto.CustomerPageQueryDto;
import com.chx.entity.Customers;
import com.chx.result.PageResult;
import com.chx.result.Result;
import com.chx.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author cp-月入过万
 * @Date 2025/5/30
 * 客户管理接口
 */
@RestController
@RequestMapping("/admin/customer")
@Api(tags = "客户相关接口")
@Slf4j
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 获取客户列表
     */
    @ApiOperation(value = "获取客户列表")
    @GetMapping("/list")
    @Cacheable(cacheNames = "customerCache",key = "'list'")
    public Result<List<Customers>> list(){
        log.info("获取客户列表");
        return Result.success(customerService.getAllCustomers());
    }
    /**
     * 新增客户
     */
    @ApiOperation(value = "新增客户")
    @PostMapping
    @CacheEvict(cacheNames = "customerCache",allEntries = true)
    public Result<String> save(@RequestBody CustomerDto customerDto){
        log.info("新增客户:{}",  customerDto);
        customerService.save(customerDto);
        return Result.success();
    }
    /**
     * 修改客户信息
     */
    @ApiOperation(value = "修改客户信息")
    @PutMapping
    @CacheEvict(cacheNames = "customerCache",allEntries = true)
    public Result<String> update(@RequestBody CustomerDto customerDto){
        log.info("修改客户信息:{}",  customerDto);
        customerService.update(customerDto);
        return Result.success();
    }
    /**
     * 根据id查找客户信息
     */
    @ApiOperation(value = "根据id查找客户信息")
    @GetMapping("/{id}")
    public Result<Customers> getById(@PathVariable Long id){
        log.info("根据id查找客户信息");
        Customers customers = customerService.getById(id);
        return Result.success(customers);
    }
    /**
     * 分页查询客户信息
     */
    @ApiOperation(value = "分页查询客户信息")
    @GetMapping("/page")
    public Result<PageResult> page(CustomerPageQueryDto customerPageQueryDto){
        log.info("分页查询客户信息");
        PageResult pageResult = customerService.page(customerPageQueryDto);
        return Result.success(pageResult);
    }
}
